<template>
  <el-dialog width="800px" :title="dialogTitle" :visible.sync="dialogVisible" @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      style="flex: 1"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="100px"
      label-position="right"
      class="submit-form"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="供应商编号" prop="supplierNo">
            <!--          <sequence-input
                          v-model="submitForm.supplierNo"
                          :placeholder="'请生成供应商编号,不填则自动生成'"
                          seq-key="SUPPLIER_NO"
                          @change="generateSupplierNo"
                        />-->
            <el-input v-model="submitForm.supplierNo" disabled placeholder="系统默认生成" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="供应商名称" prop="supplierName">
            <el-input v-model="submitForm.supplierName" placeholder="请输入供应商名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人" prop="contacts">
            <el-input v-model="submitForm.contacts" placeholder="请输入联系人名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号" prop="phoneNum">
            <el-input v-model="submitForm.phoneNum" placeholder="请输入手机号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="submitForm.email" placeholder="请输入供应商邮箱" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="传真" prop="fax">
            <el-input v-model="submitForm.fax" placeholder="请输入传真" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="state">
            <el-switch
              v-model="submitForm.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="可用"
              inactive-text="禁用"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="submitForm.address"
          maxlength="100"
          placeholder="请输入地址"
          show-word-limit
          type="textarea"
          :autosize="{ minRows: 2}"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="submitForm.remark"
          maxlength="100"
          placeholder="请输入备注"
          show-word-limit
          type="textarea"
          :autosize="{ minRows: 2}"
        />
      </el-form-item>
    </el-form>
    <!--    <div style="display: flex">
      <el-button style="flex: 1" :loading="formLoading" @click="handleClose">取消</el-button>
      <el-button style="flex: 1" :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </div>-->
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { supplier_save, supplier_update, supplier_detail } from '@/api/base-data/supplier'
/**
 * @author yizuomin
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'SubmitModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      dialogTitle: '',
      formLoading: false,
      submitForm: {
        supplierNo: '',
        supplierName: '',
        contacts: '',
        phoneNum: '',
        email: '',
        fax: '',
        address: '',
        remark: '',
        sort: '',
        state: '1'
      },
      submitFormRules: {
        supplierName: [{ required: true, message: '请输入供应商名称', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      if (!id) {
        this.dialogTitle = '新增供应商'
        return
      }
      this.dialogTitle = '修改供应商'
      this.formLoading = true
      supplier_detail(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    generateSupplierNo(supplierNo) {
      this.submitForm.supplierNo = supplierNo
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          let request = supplier_save
          if (this.submitForm.id) {
            request = supplier_update
          }
          this.formLoading = true
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleClose() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped>

</style>
